﻿#include "widget.h"
#include "ui_widget.h"
#include <QQuickWidget>
#include <QQuickView>
#include <QHBoxLayout>
#include <QQuickItem>
#include <QPushButton>
#include <QDebug>
#include "tfframe.h"

typedef QLabel MyCustomButton;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    setWindowFlags(Qt::Window|Qt::FramelessWindowHint
                   |Qt::WindowSystemMenuHint|Qt::WindowMinimizeButtonHint|Qt::WindowMaximizeButtonHint);

    metaId = qRegisterMetaType<TFFrame>("TFFrame");
    qDebug()<<Q_FUNC_INFO<<metaId;

//    useQQuickWidget();//使用QuickWidget加载qml
        useQQuickView();//使用quickview加载qml
}

Widget::~Widget()
{
    delete ui;
}

void Widget::slotRecevQmlReq(QVariant var1, QVariant var2)
{
    if(var1=="changedcolor"){
        QString style = QString("QFrame{background:%1;}").arg(var2.value<QColor>().name());
        ui->frame->setStyleSheet(style);
        return;
    }
    qDebug()<<Q_FUNC_INFO<<var1;
    TFFrame *customObj = (TFFrame*)QMetaType::create(metaId);
    if(customObj)qDebug()<<Q_FUNC_INFO<<customObj;
    QHBoxLayout *box = (QHBoxLayout*)ui->frame->layout();
    if(box){
        box->addWidget(customObj);
    }
}

void Widget::useQQuickView()
{
    QQuickView *view = new QQuickView();
    view->setFlags(Qt::FramelessWindowHint);
    view->setSurfaceType(QSurface::OpenGLSurface);
    view->setSource(QUrl::fromLocalFile("main.qml"));
    view->setResizeMode(QQuickView::SizeRootObjectToView);
    QSurfaceFormat surfaceFormat;
    surfaceFormat.setAlphaBufferSize(8);
    view->setFormat(surfaceFormat);
    view->setClearBeforeRendering(true);
    view->setColor(Qt::transparent);
    view->show();
    view->raise();
    //QWidget *createQml = QWidget::createWindowContainer(view,this);
    //    createQml->setAttribute(Qt::WA_TranslucentBackground,true);
    //QString style = QString("QWidget{background:red;}");
    //createQml->setStyleSheet(style);
    //    QHBoxLayout *hBox = new QHBoxLayout;
    //    hBox->setSpacing(0);
    //    hBox->setContentsMargins(0,0,0,0);
    //    hBox->addWidget(createQml);
    //    ui->frame->setLayout(hBox);
    QQuickItem *item = view->rootObject();
    if(item){
        QString title = (this->metaObject()->className());
        QMetaObject::invokeMethod(item,"setTitle",Q_ARG(QVariant,title));
        connect(item,SIGNAL(signalQmlOptions(QVariant,QVariant)),this,SLOT(slotRecevQmlReq(QVariant,QVariant)));
    }
}

void Widget::useQQuickWidget()
{
    QQuickWidget *view = new QQuickWidget;
    view->setSource(QUrl::fromLocalFile("main.qml"));
    view->setAttribute(Qt::WA_AlwaysStackOnTop,true);
    view->setClearColor(Qt::transparent);
    view->setResizeMode(QQuickWidget::SizeRootObjectToView);

    QHBoxLayout *hBox = new QHBoxLayout;
    hBox->setSpacing(0);
    hBox->setContentsMargins(0,0,0,0);
    hBox->addWidget(view);
    ui->frame->setLayout(hBox);

    QQuickItem *item = view->rootObject();
    if(item){
        QString title = (this->metaObject()->className());
        QMetaObject::invokeMethod(item,"setTitle",Q_ARG(QVariant,title));
        connect(item,SIGNAL(signalQmlOptions(QVariant,QVariant)),this,SLOT(slotRecevQmlReq(QVariant,QVariant)));
    }
}
